package com.fr.web.core.service;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.cache.config.CacheConfiguration;
import com.fr.base.cache.store.MemoryStoreEvictionPolicy;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.dav.Env;
import com.fr.base.file.CacheManager;
import com.fr.base.file.ConfigManager;
import com.fr.base.file.DatasourceManager;
import com.fr.base.print.PrintUtils;
import com.fr.data.impl.Connection;
import com.fr.data.impl.JDBCDatabaseConnection;
import com.fr.data.pool.ConnectionPoolManager;
import com.fr.report.ReportWebAttr;
import com.fr.report.web.EmailManager;
import com.fr.report.web.Printer;
import com.fr.schedule.ScheduleContext;
import com.fr.schedule.output.EmailNotification;
import com.fr.web.core.ServerConstants;
import com.fr.web.core.WebUtils;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServerConfigService extends NoSessionIDService
{
  private static ServerConfigService SERVER_SERVICE = new ServerConfigService();

  public static ServerConfigService getInstance()
  {
    return SERVER_SERVICE;
  }

  public boolean accept(String paramString)
  {
    return paramString.startsWith("fr_server");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString)
    throws Exception
  {
    if (paramString == null)
      return;
    if ("fr_server_testmail".equals(paramString))
      dealWithTestMail(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_server_emailattrset".equals(paramString))
      dealWithEmailAttrSet(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_server_getmailinfo".equals(paramString))
      dealWithGetMailInfo(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_server_getcacheinfo".equals(paramString))
      dealWithGetCacheInfo(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_server_cacheattrset".equals(paramString))
      dealWithCacheAttrSet(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_server_getconfiginfo".equals(paramString))
      dealWithGetConfigInfo(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_server_configattrset".equals(paramString))
      dealWithConfigAttrSet(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_server_getconpoolinfo".equals(paramString))
      dealWithGetConPoolInfo(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_server_getconnectioninfo".equals(paramString))
      dealWithGetConnectionInfo(paramHttpServletRequest, paramHttpServletResponse);
    else if ("fr_server_connectionattrset".equals(paramString))
      dealWithConnectionAttrSet(paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void dealWithTestMail(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    JSONObject localJSONObject = null;
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    String str = "success";
    try
    {
      localJSONObject = new JSONObject(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "emailToAttr"));
      EmailManager localEmailManager = new EmailManager();
      localEmailManager.setMailHost(localJSONObject.getString("serverName"));
      localEmailManager.setUser(localJSONObject.getString("userName"));
      localEmailManager.setPassword(localJSONObject.getString("passWord"));
      localEmailManager.setFromEmailAddress(localJSONObject.getString("fromAddress"));
      localEmailManager.sendTestMail(localJSONObject.getString("toAddress"));
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      str = localException.getMessage();
    }
    localPrintWriter.write(str);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void dealWithEmailAttrSet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    JSONObject localJSONObject = null;
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    String str = "success";
    try
    {
      localJSONObject = new JSONObject(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "emailAttrJson"));
      EmailNotification.getEmailManager().parseJSON(localJSONObject);
      FRContext.getCurrentEnv().writeResource(FRContext.getConfigManager());
    }
    catch (JSONException localJSONException)
    {
      FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
      str = localJSONException.getMessage();
    }
    localPrintWriter.write(str);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void dealWithGetMailInfo(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    JSONObject localJSONObject = new JSONObject();
    String str1 = EmailNotification.getEmailManager().getMailHost();
    String str2 = EmailNotification.getEmailManager().getUser();
    String str3 = EmailNotification.getEmailManager().getPassword();
    String str4 = EmailNotification.getEmailManager().getFromEmailAddress();
    localJSONObject.put("serverName", str1);
    localJSONObject.put("userName", str2);
    localJSONObject.put("passWord", str3);
    localJSONObject.put("fromAddress", str4);
    localPrintWriter.print(localJSONObject);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void dealWithGetCacheInfo(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    JSONObject localJSONObject = new JSONObject();
    int i = FRContext.getCacheManager().getDbConfig().getMaxElementsInMemory();
    MemoryStoreEvictionPolicy localMemoryStoreEvictionPolicy = FRContext.getCacheManager().getDbConfig().getMemoryStoreEvictionPolicy();
    long l1 = FRContext.getCacheManager().getDbConfig().getTimeToIdleSeconds();
    long l2 = FRContext.getCacheManager().getDbConfig().getTimeToLiveSeconds();
    boolean bool = FRContext.getCacheManager().isAlwaysReloadTpl();
    localJSONObject.put("maxElementsInMemory", i);
    localJSONObject.put("memoryStoreEvictionPolicy", localMemoryStoreEvictionPolicy);
    localJSONObject.put("timeToIdleSeconds", l1);
    localJSONObject.put("timeToLiveSeconds", l2);
    localJSONObject.put("alwaysReloadTpl", bool);
    localPrintWriter.print(localJSONObject);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void dealWithCacheAttrSet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    JSONObject localJSONObject = new JSONObject();
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    String str = "success";
    try
    {
      localJSONObject = new JSONObject(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "cacheAttrJson"));
      FRContext.getCacheManager().getDbConfig().parseJSON(localJSONObject);
      FRContext.getCacheManager().setAlwaysReloadTpl(localJSONObject.getBoolean("alwaysReloadTpl"));
      FRContext.getCurrentEnv().writeResource(FRContext.getConfigManager());
    }
    catch (JSONException localJSONException)
    {
      FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
      str = localJSONException.getMessage();
    }
    localPrintWriter.write(str);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void dealWithGetConfigInfo(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    JSONObject localJSONObject = new JSONObject();
    Object[] arrayOfObject = FRContext.getDatasourceManager().getConnectionNameArray();
    String str1 = FRContext.getDatasourceManager().getScheduleDatasource();
    String str2 = "reportlets";
    String str3 = "resources";
    String str4 = FRContext.getConfigManager().getServerCharset();
    String[] arrayOfString1 = ServerConstants.ENCODING_ARRAY;
    String[] arrayOfString2 = { "", Inter.getLocText("Simplified_Chinese_Language"), Inter.getLocText("English_Language"), Inter.getLocText("Japanese_Language"), Inter.getLocText("Traditional_Chinese_Language") };
    String str5 = arrayOfString2[FRContext.getConfigManager().getServerLanguage()];
    String str6 = FRContext.getConfigManager().getServletMapping();
    boolean bool1 = FRContext.getConfigManager().isSupportGzip();
    boolean bool2 = FRContext.getConfigManager().isPopupFlashPrintSetting();
    boolean bool3 = FRContext.getConfigManager().isPopupPdfPrintSetting();
    Level[] arrayOfLevel = { Level.SEVERE, Level.WARNING, Level.INFO };
    Level localLevel = FRContext.getConfigManager().getServerLogLevel();
    int i = FRContext.getConfigManager().getConcurrency();
    int j = FRContext.getConfigManager().getConcurrencyOnce();
    String[] arrayOfString3 = PrintUtils.getSystemPrinterNameArray();
    String[] arrayOfString4 = FRContext.getConfigManager().getReportWebAttr().getPrinter().getPrinters();
    localJSONObject.put("connectionNameArray", arrayOfObject);
    localJSONObject.put("scheduleDatasource", str1);
    localJSONObject.put("reportletsName", str2);
    localJSONObject.put("resourcesName", str3);
    localJSONObject.put("serverCharSet", str4);
    localJSONObject.put("encodingArray", arrayOfString1);
    localJSONObject.put("serverLanguageArray", arrayOfString2);
    localJSONObject.put("serverLanguage", str5);
    localJSONObject.put("serverletMapping", str6);
    localJSONObject.put("isSupportGzip", bool1);
    localJSONObject.put("isPopupFlashPrintSetting", bool2);
    localJSONObject.put("isPopupPdfPrintSetting", bool3);
    localJSONObject.put("LOG_LEVELS", arrayOfLevel);
    localJSONObject.put("serverLogLevel", localLevel);
    localJSONObject.put("concurrency", i);
    localJSONObject.put("concurrencyOnce", j);
    localJSONObject.put("serverPrinterList", arrayOfString3);
    localJSONObject.put("serverActivePrinterList", arrayOfString4);
    localPrintWriter.print(localJSONObject);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void dealWithConfigAttrSet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    JSONObject localJSONObject = new JSONObject();
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    String str1 = "success";
    try
    {
      localJSONObject = new JSONObject(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "configAttrJson"));
      FRContext.getDatasourceManager().setScheduleDatasource(localJSONObject.getString("scheduleDatasource"));
      FRContext.getConfigManager().setServerCharset(localJSONObject.getString("serverCharSet"));
      String[] arrayOfString1 = { "", Inter.getLocText("Simplified_Chinese_Language"), Inter.getLocText("English_Language"), Inter.getLocText("Japanese_Language"), Inter.getLocText("Traditional_Chinese_Language") };
      FRContext.getConfigManager().setServerLanguage(localJSONObject.getInt("serverLanguage"));
      FRContext.getConfigManager().setServletMapping(localJSONObject.getString("serverletMapping"));
      FRContext.getConfigManager().setSupportGzip(localJSONObject.getBoolean("isSupportGzip"));
      FRContext.getConfigManager().setPopupFlashPrintSetting(localJSONObject.getBoolean("isPopupFlashPrintSetting"));
      FRContext.getConfigManager().setPopupPdfPrintSetting(localJSONObject.getBoolean("isPopupPdfPrintSetting"));
      Level localLevel = Level.parse(localJSONObject.getString("serverLogLevel"));
      FRContext.getConfigManager().setServerLogLevel(localLevel);
      FRContext.getConfigManager().setConcurrency(localJSONObject.getInt("concurrency"));
      FRContext.getConfigManager().setConcurrencyOnce(localJSONObject.getInt("concurrencyOnce"));
      String str2 = localJSONObject.getString("serverActivePrinterList");
      String[] arrayOfString2 = new String[0];
      if (str2.equals(""))
        break label248:
      arrayOfString2 = str2.substring(0, str2.length() - 1).split(",");
      label248: FRContext.getConfigManager().getReportWebAttr().getPrinter().setPrinters(arrayOfString2);
      FRContext.getCurrentEnv().writeResource(FRContext.getConfigManager());
      FRContext.getCurrentEnv().writeResource(FRContext.getDatasourceManager());
      ScheduleContext.refreshDatabaseConnection();
    }
    catch (JSONException localJSONException)
    {
      FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
      str1 = localJSONException.getMessage();
    }
    localPrintWriter.write(str1);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private static void dealWithGetConPoolInfo(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    JSONObject localJSONObject = new JSONObject();
    String[] arrayOfString1 = ConnectionPoolManager.getConnectionPoolManager().getPoolManagerInfo();
    String[] arrayOfString2 = ConnectionPoolManager.getConnectionPoolManager().getLogs();
    localJSONObject.put("poolInfo", arrayOfString1);
    localJSONObject.put("runningCon", arrayOfString2);
    localPrintWriter.print(localJSONObject);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private void dealWithGetConnectionInfo(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    Iterator localIterator = FRContext.getDatasourceManager().getConnectionNameIterator();
    JSONArray localJSONArray = new JSONArray();
    while (true)
    {
      String str;
      Connection localConnection;
      do
      {
        if (!(localIterator.hasNext()))
          break label156;
        str = (String)localIterator.next();
        localConnection = FRContext.getDatasourceManager().getConnection(str);
      }
      while (!(localConnection instanceof JDBCDatabaseConnection));
      JSONObject localJSONObject = new JSONObject();
      localJSONObject.put("name", str);
      localJSONObject.put("driver", ((JDBCDatabaseConnection)localConnection).getDriver());
      localJSONObject.put("url", ((JDBCDatabaseConnection)localConnection).getURL());
      localJSONObject.put("user", ((JDBCDatabaseConnection)localConnection).getUser());
      localJSONObject.put("password", ((JDBCDatabaseConnection)localConnection).getPassword());
      localJSONArray.put(localJSONObject);
    }
    label156: localPrintWriter.print(localJSONArray);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  private void dealWithConnectionAttrSet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    JSONObject localJSONObject = new JSONObject();
    JSONArray localJSONArray = new JSONArray();
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    String str1 = "success";
    try
    {
      localJSONObject = new JSONObject(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "connectionAttrJson"));
      localJSONArray = new JSONArray(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "changedConName"));
      for (int i = 0; i < localJSONArray.length(); ++i)
      {
        String str2 = localJSONArray.getString(i);
        JDBCDatabaseConnection localJDBCDatabaseConnection = (JDBCDatabaseConnection)FRContext.getDatasourceManager().getConnection(str2);
        if (localJSONObject.getJSONObject(str2).has("driver"))
          localJDBCDatabaseConnection.setDriver(localJSONObject.getJSONObject(str2).getString("driver"));
        if (localJSONObject.getJSONObject(str2).has("url"))
          localJDBCDatabaseConnection.setURL(localJSONObject.getJSONObject(str2).getString("url"));
        if (localJSONObject.getJSONObject(str2).has("user"))
          localJDBCDatabaseConnection.setUser(localJSONObject.getJSONObject(str2).getString("user"));
        if (localJSONObject.getJSONObject(str2).has("password"))
          localJDBCDatabaseConnection.setPassword(localJSONObject.getJSONObject(str2).getString("password"));
      }
      FRContext.getCurrentEnv().writeResource(FRContext.getDatasourceManager());
    }
    catch (JSONException localJSONException)
    {
      FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
      str1 = localJSONException.getMessage();
    }
    localPrintWriter.write(str1);
    localPrintWriter.flush();
    localPrintWriter.close();
  }
}